package com.example.library_common.album.ui

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.example.library_common.R
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.bean.AlbumFolder
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class AlbumFolderFragment : SimpleListFragment<AlbumFolder>(), OnItemChildClickListener {

    companion object {
        @JvmStatic
        fun newInstance(): AlbumFolderFragment {
            val args = Bundle()
            val fragment = AlbumFolderFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var albumFolderSelector: AlbumFolderSelector? = null

    private var selectedPos: Int = 0

    fun setAlbumFolderSelector(listener: AlbumFolderSelector?) {
        this.albumFolderSelector = listener
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAlwaysRefresh = false
        isInitialRefresh = false
        isEnableLoadMore = false
    }

    override fun addItemDecoration(): RecyclerView.ItemDecoration {
        val decoration = DividerItemDecoration(_mActivity, LinearLayoutManager.VERTICAL)
        decoration.setDrawable(
            ColorDrawable(
                ContextCompat.getColor(
                    _mActivity,
                    R.color.color_C9fffffff
                )
            )
        )
        return decoration
    }

    override fun getItemLayout() = R.layout.album_folder_fragment_item_layout

    override fun convertItem(helper: JssBaseViewHolder?, item: AlbumFolder?) {
        val path = if (item?.albumFiles?.isNotEmpty() == true)
            item.albumFiles?.get(0)?.path
        else "#"
        helper?.setImageLocalUrl(R.id.albumFolderAvatar, path, R.drawable.img_folder_bg_rect)
            ?.setText(R.id.albumFolderName, item?.name)
            ?.setCheckBoxChecked(R.id.albumFolderChecked,helper.layoutPosition==selectedPos)
    }

    override fun initView(view: View) {
        super.initView(view)
        mAdapter.setOnItemChildClickListener(this)
        mAdapter.addChildClickViewIds(R.id.albumFolderChecked)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .endSubType().build()
    }

    public fun loadListData(data: Collection<AlbumFolder>, selectedPos: Int) {
        this.selectedPos = selectedPos
        loadListDate(data)
    }

    override fun netRequest() {

    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        albumFolderSelector?.onSelector(position)
    }

    interface AlbumFolderSelector {
        fun onSelector(position: Int)
    }

}